"""Build .docx from paper.md + all output tables."""

import re
from pathlib import Path
from docx import Document
from docx.shared import Inches, Pt, Cm, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.oxml.ns import qn

PROJECT = Path("/mnt/c/demographics_capital_flows/crises")
TABLES_DIR = PROJECT / "output" / "tables"
PAPER_MD = None
for _name in ["crises_paper_20260221_v2.md", "crises_paper_20260221.md", "paper.md"]:
    _p = PROJECT / "paper" / _name
    if _p.exists():
        PAPER_MD = _p
        break
if PAPER_MD is None:
    PAPER_MD = PROJECT / "paper" / "paper.md"
OUTPUT = PROJECT / "paper" / "crises_paper_20260305_r3.docx"

# ── helpers ──────────────────────────────────────────────────────────

def set_cell_text(cell, text, bold=False, size=Pt(9), font_name='Times New Roman'):
    """Set cell text with formatting."""
    cell.text = ""
    p = cell.paragraphs[0]
    p.alignment = WD_ALIGN_PARAGRAPH.CENTER
    run = p.add_run(text)
    run.font.size = size
    run.font.name = font_name
    run.bold = bold

def add_md_table(doc, md_text, title=None):
    """Parse a markdown table and add it as a Word table."""
    lines = [l.strip() for l in md_text.strip().split('\n') if l.strip()]
    table_lines = [l for l in lines if '|' in l and not l.startswith('#')]
    if not table_lines:
        return

    rows = []
    for line in table_lines:
        cells = [c.strip() for c in line.split('|')]
        cells = [c for c in cells if c != '']
        if all(set(c) <= set('-: ') for c in cells):
            continue
        rows.append(cells)

    if len(rows) < 2:
        return

    if title:
        p = doc.add_paragraph()
        run = p.add_run(title)
        run.bold = True
        run.font.size = Pt(11)
        run.font.name = 'Times New Roman'

    n_cols = max(len(r) for r in rows)
    table = doc.add_table(rows=len(rows), cols=n_cols)
    table.style = 'Light Shading'
    table.alignment = WD_TABLE_ALIGNMENT.CENTER

    for i, row_data in enumerate(rows):
        for j, cell_text in enumerate(row_data):
            if j < n_cols:
                is_header = (i == 0)
                set_cell_text(table.cell(i, j), cell_text, bold=is_header)

    doc.add_paragraph()


def parse_md_file(filepath):
    """Parse a .md table file into sections."""
    text = filepath.read_text()
    sections = []

    parts = re.split(r'(```[\s\S]*?```)', text)

    for part in parts:
        if part.startswith('```'):
            content = part.strip('`').strip()
            if content.startswith('\n'):
                content = content[1:]
            sections.append(('irf', content))
        else:
            lines = part.split('\n')
            current_title = None
            table_buf = []

            for line in lines:
                if line.startswith('#'):
                    if table_buf:
                        sections.append(('table', current_title, '\n'.join(table_buf)))
                        table_buf = []
                    current_title = re.sub(r'^#+\s*', '', line).strip()
                elif '|' in line:
                    table_buf.append(line)
                elif line.strip().startswith('*') and not table_buf:
                    sections.append(('note', line.strip()))

            if table_buf:
                sections.append(('table', current_title, '\n'.join(table_buf)))

    return sections


# ── main ──────────────────────────────────────────────────────────────

def build_docx():
    doc = Document()

    for section in doc.sections:
        section.top_margin = Cm(2.54)
        section.bottom_margin = Cm(2.54)
        section.left_margin = Cm(2.54)
        section.right_margin = Cm(2.54)

    style = doc.styles['Normal']
    style.font.name = 'Times New Roman'
    style.font.size = Pt(12)
    style.paragraph_format.space_after = Pt(6)
    style.paragraph_format.line_spacing = 1.15

    # ── Parse paper.md and render ──
    paper_text = PAPER_MD.read_text()
    lines = paper_text.split('\n')

    i = 0
    while i < len(lines):
        line = lines[i]

        # Skip horizontal rules
        if line.strip() == '---':
            i += 1
            continue

        # Skip footnote definitions
        if re.match(r'^\[\^', line):
            i += 1
            continue

        if line.startswith('# ') and not line.startswith('## '):
            title = line[2:].strip()
            p = doc.add_heading(title, level=0)
            p.alignment = WD_ALIGN_PARAGRAPH.CENTER
            for run in p.runs:
                run.font.size = Pt(16)
                run.font.name = 'Times New Roman'
                run.font.color.rgb = RGBColor(0, 0, 0)
            i += 1
            continue

        # Abstract heading — render centered
        if line.startswith('## Abstract'):
            p = doc.add_heading('Abstract', level=1)
            p.alignment = WD_ALIGN_PARAGRAPH.CENTER
            for run in p.runs:
                run.font.size = Pt(14)
                run.font.name = 'Times New Roman'
                run.font.color.rgb = RGBColor(0, 0, 0)
            i += 1
            continue

        if line.startswith('## '):
            heading = line[3:].strip()
            p = doc.add_heading(heading, level=1)
            for run in p.runs:
                run.font.size = Pt(14)
                run.font.name = 'Times New Roman'
                run.font.color.rgb = RGBColor(0, 0, 0)
            i += 1
            continue

        if line.startswith('### '):
            heading = line[4:].strip()
            p = doc.add_heading(heading, level=2)
            for run in p.runs:
                run.font.size = Pt(12)
                run.font.name = 'Times New Roman'
                run.font.color.rgb = RGBColor(0, 0, 0)
            i += 1
            continue

        if line.strip().startswith('$$'):
            stripped = line.strip()
            # Single-line display math: $$...$$
            if stripped.endswith('$$') and len(stripped) > 4 and stripped.count('$$') == 2:
                math_text = stripped[2:-2].strip()
                i += 1
            else:
                # Multi-line display math: $$ on its own line
                math_lines = [stripped.replace('$$', '')]
                i += 1
                while i < len(lines) and '$$' not in lines[i]:
                    math_lines.append(lines[i].strip())
                    i += 1
                if i < len(lines):
                    math_lines.append(lines[i].strip().replace('$$', ''))
                    i += 1
                math_text = ' '.join(l for l in math_lines if l)
            for old, new in [('\\text{', ''), ('\\log', 'log'),
                             ('\\cdot', '\u00b7'), ('\\cdots', '...'),
                             ('\\varepsilon', '\u03b5'), ('\\alpha', '\u03b1'),
                             ('\\beta', '\u03b2'), ('\\gamma', '\u03b3'),
                             ('\\delta', '\u03b4'), ('\\Delta', '\u0394'),
                             ('\\hat', ''), ('\\widehat', ''),
                             ('\\sum', '\u03a3'), ('\\exp', 'exp'),
                             ('\\times', '\u00d7'), ('\\phi', '\u03c6'),
                             ('\\prime', '\u2032'), ('\\Lambda', '\u039b'),
                             ('\\Pr', 'Pr'), ('\\leq', '\u2264'),
                             ('\\geq', '\u2265'),
                             ('\\left(', '('), ('\\right)', ')'),
                             ('\\left[', '['), ('\\right]', ']'),
                             ('\\div', '\u00f7'), ('\\tau', '\u03c4'),
                             ('\\mu', '\u03bc'), ('\\sigma', '\u03c3'),
                             ('\\pi', '\u03c0'), ('\\theta', '\u03b8'),
                             ('\\rho', '\u03c1'), ('\\lambda', '\u03bb'),
                             ('\\omega', '\u03c9'), ('\\eta', '\u03b7'),
                             ('\\in', '\u2208'),
                             ('\\{', '{'), ('\\}', '}'),
                             ('\\_', '_')]:
                math_text = math_text.replace(old, new)
            math_text = re.sub(r'_\{([^}]+)\}', r'_\1', math_text)
            math_text = re.sub(r'\^\{([^}]+)\}', r'^\1', math_text)
            math_text = math_text.replace('{', '').replace('}', '')
            p = doc.add_paragraph()
            p.alignment = WD_ALIGN_PARAGRAPH.CENTER
            run = p.add_run(math_text)
            run.font.size = Pt(10)
            run.font.name = 'Cambria Math'
            run.italic = True
            continue

        if '|' in line and line.strip().startswith('|'):
            table_lines = []
            while i < len(lines) and '|' in lines[i]:
                table_lines.append(lines[i])
                i += 1
            add_md_table(doc, '\n'.join(table_lines))
            continue

        if line.strip():
            p = doc.add_paragraph()
            text = line.strip()
            # Pre-process significance stars (e.g., 8.9***) so they don't
            # interfere with markdown bold/italic parsing
            text = re.sub(r'(\d)\*\*\*', r'\1⁂⁂⁂', text)
            text = re.sub(r'(\d)\*\*(?!\*)', r'\1⁂⁂', text)
            text = re.sub(r'(\d)\*(?!\*)', r'\1⁂', text)
            parts = re.split(r'(\*\*\*[^*]+\*\*\*|\*\*[^*]+\*\*|\*[^*]+\*|\$[^$]+\$)', text)
            for part in parts:
                if part.startswith('***') and part.endswith('***'):
                    run = p.add_run(part[3:-3])
                    run.bold = True
                    run.italic = True
                    run.font.name = 'Times New Roman'
                    run.font.size = Pt(12)
                elif part.startswith('**') and part.endswith('**'):
                    run = p.add_run(part[2:-2])
                    run.bold = True
                    run.font.name = 'Times New Roman'
                    run.font.size = Pt(12)
                elif part.startswith('*') and part.endswith('*') and len(part) > 2:
                    run = p.add_run(part[1:-1])
                    run.italic = True
                    run.font.name = 'Times New Roman'
                    run.font.size = Pt(12)
                elif part.startswith('$') and part.endswith('$'):
                    math = part[1:-1]
                    for old, new in [('\\hat{\\beta}', '\u03b2\u0302'),
                                     ('\\hat{\\gamma}', '\u03b3\u0302'),
                                     ('\\hat\\gamma', '\u03b3\u0302'),
                                     ('\\widehat', ''), ('\\hat', ''),
                                     ('\\beta', '\u03b2'), ('\\gamma', '\u03b3'),
                                     ('\\Delta', '\u0394'), ('\\times', '\u00d7'),
                                     ('\\varepsilon', '\u03b5'), ('\\alpha', '\u03b1'),
                                     ('\\delta', '\u03b4'), ('\\phi', '\u03c6'),
                                     ('\\Lambda', '\u039b'), ('\\Pr', 'Pr'),
                                     ('\\log', 'log'), ('\\exp', 'exp'),
                                     ('\\text{', ''),
                                     ('\\sum', '\u03a3'), ('\\cdot', '\u00b7'),
                                     ('\\cdots', '...'),
                                     ('\\approx', '\u2248'), ('\\leq', '\u2264'),
                                     ('\\geq', '\u2265'), ('\\neq', '\u2260'),
                                     ('\\sim', '~'), ('\\prime', '\u2032'),
                                     ('\\left(', '('), ('\\right)', ')'),
                                     ('\\left[', '['), ('\\right]', ']'),
                                     ('\\div', '\u00f7'), ('\\tau', '\u03c4'),
                                     ('\\mu', '\u03bc'), ('\\sigma', '\u03c3'),
                                     ('\\pi', '\u03c0'), ('\\theta', '\u03b8'),
                                     ('\\rho', '\u03c1'), ('\\lambda', '\u03bb'),
                                     ('\\omega', '\u03c9'), ('\\eta', '\u03b7'),
                                     ('\\in', '\u2208'),
                                     ('\\{', '{'), ('\\}', '}'),
                                     ('\\_', '_')]:
                        math = math.replace(old, new)
                    math = re.sub(r'_\{([^}]+)\}', r'_\1', math)
                    math = re.sub(r'\^\{([^}]+)\}', r'^\1', math)
                    math = math.replace('{', '').replace('}', '')
                    run = p.add_run(math)
                    run.italic = True
                    run.font.name = 'Cambria Math'
                    run.font.size = Pt(11)
                else:
                    # Restore significance stars
                    restored = part.replace('⁂⁂⁂', '***').replace('⁂⁂', '**').replace('⁂', '*')
                    run = p.add_run(restored)
                    run.font.name = 'Times New Roman'
                    run.font.size = Pt(12)

        i += 1

    # ── Page break before appendix tables ──
    doc.add_page_break()
    p = doc.add_heading('Appendix: Tables', level=0)
    p.alignment = WD_ALIGN_PARAGRAPH.CENTER
    for run in p.runs:
        run.font.size = Pt(16)
        run.font.name = 'Times New Roman'
        run.font.color.rgb = RGBColor(0, 0, 0)

    # ── Add all tables ──
    table_files = [
        ("Table 1: Summary Statistics", "summary_statistics.md"),
        ("Table 2: Crisis Prediction — Banking Crisis Onset", "crisis_prediction.md"),
        ("Table 3: Crisis Prediction by Type", "crisis_by_type.md"),
        ("Table 4: Incremental R²", "incremental_r2.md"),
        ("Table 5: Sudden Stops & CA Reversals", "sudden_stops.md"),
        ("Table 6: CA Reversal Channels — Youth vs. Aging", "reversal_channels.md"),
        ("Table 7: CA Reversals by Demographic Stage", "reversal_terciles.md"),
        ("Table 8: Post-Reversal Recovery Speed", "recovery_speed.md"),
        ("Table 9: NFA × Crisis Type Cross-Tabulation", "nfa_crisis_crosstab.md"),
        ("Table 10: Demographic Stage × Crisis Type", "demo_crisis_crosstab.md"),
        ("Table 11: Aging and Bank Risk Channel", "aging_bank_risk.md"),
        ("Table 12: Post-Crisis Cumulative Output Loss", "post_crisis_recovery.md"),
        ("Table 13: Crisis Duration and Demographics", "crisis_duration.md"),
        ("Table 14: Crisis Prediction by Capital Account Openness", "crisis_by_openness.md"),
        ("Table 15: Out-of-Sample Prediction", "out_of_sample.md"),
        ("Table 16: Forward Vulnerability Rankings", "vulnerability_rankings.md"),
        ("Table 17: Demographic Trajectory", "demographic_trajectory.md"),
        ("Table 18: Logit vs. LPM Comparison", "logit_vs_lpm.md"),
        ("Table 19: Lagged Demographics", "lagged_demographics.md"),
        ("Table 20: OECD Banking Crisis Prediction", "oecd_banking.md"),
        ("Table 21: OECD CA Reversal Prediction", "oecd_reversal.md"),
        ("Table 22: OECD Aging/Youth Channel", "oecd_aging_channel.md"),
        ("Table 23: KAOPEN × Banking Crisis", "kaopen_banking.md"),
        ("Table 24: KAOPEN × CA Reversal", "kaopen_reversal.md"),
        ("Table 25: Youth/Aging Channels by KAOPEN", "kaopen_channels.md"),
        ("Table 26: Crisis Incidence by KAOPEN Tercile", "kaopen_summary.md"),
        ("Table 27: Logit/Cloglog Primary Estimator + Proper Scoring", "phase8_logit_primary.md"),
        ("Table 27a: Logit Marginal Effects", "phase8_logit_regression.md"),
        ("Table 28: Tightened Reversal Definitions", "phase8_reversal_definitions.md"),
        ("Table 29: Alternative Severity Metrics", "phase8_severity_metrics.md"),
        ("Table 30: Middle-Openness Compositional Test", "phase8_middle_openness.md"),
        ("Table 31: Table 19 Verification + Youth Sign Flip VIF", "phase8_verification.md"),
        # Phase 9: Capital Flow Risk
        ("Table 34: Residual Distribution Moments by KAOPEN Tercile", "residual_moments.md"),
        ("Table 35: Quantile Regression — Demographics Across the CA/GDP Distribution", "quantile_regression.md"),
        ("Table 36: Risk Premium of Financial Opening", "opening_risk_premium.md"),
        ("Table 37: Tail Risk Matrix — Demographic Stage × Capital Account Openness", "risk_matrix.md"),
        ("Table 38: Forward CA/GDP Vulnerability Rankings", "var_vulnerability.md"),
        ("Table 39: Systemic Capital Flow Risk — Concentration and Closure Counterfactuals", "systemic_risk.md"),
        # Phase 10: Financial Structure
        ("Table 40: Financial Structure × Demographics → Crisis Onset", "fin_structure_crisis.md"),
        ("Table 41: Financial Structure × Demographics → CA Reversals", "fin_structure_reversal.md"),
        ("Table 42: Financial Structure × Demographics → Crisis Severity", "fin_structure_severity.md"),
        ("Table 43: Risk Matrix — Demographic Stage × Financial Structure", "fin_structure_risk_matrix.md"),
    ]

    for table_num, (label, filename) in enumerate(table_files):
        filepath = TABLES_DIR / filename
        if not filepath.exists():
            continue

        doc.add_page_break()

        p = doc.add_heading(label, level=1)
        for run in p.runs:
            run.font.size = Pt(13)
            run.font.name = 'Times New Roman'
            run.font.color.rgb = RGBColor(0, 0, 0)

        sections = parse_md_file(filepath)

        for section in sections:
            if section[0] == 'table':
                _, title, md = section
                if title and title != label:
                    add_md_table(doc, md, title=title)
                else:
                    add_md_table(doc, md)
            elif section[0] == 'note':
                p = doc.add_paragraph()
                run = p.add_run(section[1])
                run.italic = True
                run.font.size = Pt(9)
                run.font.name = 'Times New Roman'

    doc.save(str(OUTPUT))
    print(f"Saved: {OUTPUT}")
    print(f"Size: {OUTPUT.stat().st_size / 1024:.0f} KB")


if __name__ == '__main__':
    build_docx()
